<?php
header('Content-disposition: attachment; filename=q1_'.date("Y_W", time()-86400*7).'_all.csv');
header("Content-type: text/csv");
header("Pragma: no-cache");
header("Expires: 0");
mysql_connect('localhost', 'root', '');
mysql_select_db('unwdmi');
include('../inc/functions.inc.php');

$sql = "SELECT s.stn AS stn, name, country, latitude ,longitude, elevation,
				AES_DECRYPT(temp,'@#dwave\$r') AS temp,
						AES_DECRYPT(dewp,'@#dwave\$r') AS dewp,
						AES_DECRYPT(stp,'@#dwave\$r') AS stp,
						AES_DECRYPT(slp,'@#dwave\$r') AS slp,
						AES_DECRYPT(visib,'@#dwave\$r') AS visib,
						AES_DECRYPT(wdsp,'@#dwave\$r') AS wdsp,
						AES_DECRYPT(prcp,'@#dwave\$r') AS prcp,
						AES_DECRYPT(sndp,'@#dwave\$r') AS sndp,
						AES_DECRYPT(frshtt,'@#dwave\$r') AS frshtt,
						AES_DECRYPT(cldc,'@#dwave\$r') AS cldc,
						AES_DECRYPT(wnddir,'@#dwave\$r') AS wnddir, EXTRACT( WEEK FROM w.date ) AS week, EXTRACT( YEAR FROM w.date ) AS year
					FROM stations AS s, weeklyhistory AS w
					WHERE s.stn = w.stn
					AND DATEDIFF(w.date, DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE()) DAY)) < 28
					ORDER BY  year DESC, week DESC, s.stn ASC";
$result = mysql_query($sql);

echo "Week;Year;Station ID;Name;Country;Latitude;Longitude;Elevation;Temperature;Dewpoint;Station Level Pressure;Sea Level Pressure; Visibility;Windspeed;Precipitation;Snowfall;Events;Cloud Coverage;Winddirection\n";

while($row = mysql_fetch_assoc($result)) {
	echo $row['week'].';';
	echo $row['year'].';';
	echo $row['stn'].';';
	echo $row['name'].';';
	echo $row['country'].';';
	echo $row['latitude'].';';
	echo $row['longitude'].';';
	echo $row['elevation'].';';
	echo $row['temp'].';';
	echo $row['dewp'].';';
	echo $row['stp'].';';
	echo $row['slp'].';';
	echo $row['visib'].';';
	echo $row['wdsp'].';';
	echo $row['prcp'].';';
	echo $row['sndp'].';';
	echo readFRSHTTnoHTML($row['frshtt']).';';
	echo $row['cldc'].';';
	echo $row['wnddir'];
	echo "\n";			
}
?>